<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class HomeAction extends Action
{

	protected $_user = array();
	protected $_userGroup = array();
	protected $_adminGroup = array();
	protected $_global = array();
	protected $_User;

	function _initialize()
	{
		
		$this->_User = D('User');
	    $this->_global = F('cache_site_configs');
	    
	    if (empty($this->_global))
	    {
	    	$Config = D('Config');
	    	$Config->updateConfigs();
	    	$this->_global  = F('cache_site_configs');
	    }

		$userCookie = cookie('user');
		if (empty($userCookie))
		{
			$this->_user = array(
			    'user_id'     => 0,
			    'username'    => 'guest',
			    'user_group'  => 6,
			    'admin_group' => 0,
			    'lang'        => '',
			    'theme'       => '',    
			);
		}
		else
		{
			list($userID, $username) = explode("\t", kg_auth_code($userCookie['auth'], 'decode'));
			if ($userID != $userCookie['user_id'])
			{
				exit('Hacking attempts...');
			}
			$this->_user = $this->_User->find($userCookie['user_id']);
		}

		$language = empty($this->_user['lang']) ? $this->_global['default_lang'] : $this->_user['lang'];
		$template = empty($this->_user['theme']) ? $this->_global['default_theme'] : $this->_user['theme'];

		if (!cookie('think_language'))
		{
		    cookie('think_language', $language, 30 * 86400);
		}
		if (!cookie('think_template'))
		{
		    cookie('think_template', $template, 30 * 86400);
		}

		$this->_userGroup  = F('cache_usergroup_'. $this->_user['user_group']);
		$this->_adminGroup = F('cache_admingroup_'. $this->_user['admin_group']);

		D('UserSession')->updateSession($this->_user['user_id']);

		$this->assign('_user', $this->_user);
		$this->assign('_adminGroup', $this->_adminGroup);
		$this->assign('_userGroup', $this->_userGroup);
		$this->assign('_global', $this->_global);

		if($this->_global['gzip_enable'] && function_exists('ob_gzhandler'))
		{
			ob_start('ob_gzhandler');
		}
		else
		{
			ob_start();
		}

		$categories = F('cache_categories');
		if (!empty($categories))
		{
			foreach ($categories as $value)
			{
				if (0 == $value['parent'])
				{
					$topCategories[] = $value;
				}
			}
		}

		$navigations = F('cache_navigations');
		$links = F('cache_links');
		$jsLangPackage = json_encode(L('js_lang_package'));

		$this->assign('jsLangPackage', $jsLangPackage);
		$this->assign('_topCategories', $topCategories);
		$this->assign('_navigations', $navigations);
		$this->assign('_links', $links);
		$this->assign('waitSecond', 3);

		/*
		$Cron = A('Cron');
		$Cron->runCron();
		*/

		if (!$this->_global['bot_enable'] && is_bot())
		{
			exit(header("HTTP/1.1 403 Forbidden"));
		}
		
		if (!in_array($this->_user['admin_group'], array(1,2,3)) && $this->_global['site_disable'])
		{
			$message = !empty($this->_global['site_disable_msg']) ? $this->_global['site_disable_msg'] : L('site_disabled');
		    $this->error($message);
		}

		if (!$this->_userGroup['allow_visit'])
		{
			$this->error(L('no_visit_perms'));
		}

		$ip = get_client_ip();
		if (!is_valid_ip($ip) && !in_array($this->_user['admin_group'], array(1,2,3)))
		{
		    L('ip_not_allowed_visit', sprintf(L('ip_not_allowed_visit'), $ip));
            $this->error(L('ip_not_allowed_visit'));
		}

		if(!empty($this->_global['load_limit']) && substr(PHP_OS, 0, 3) != 'WIN')
		{
			if($fp = @fopen('/proc/loadavg', 'r'))
			{
				list($loadAverage) = explode(' ', fread($fp, 6));
				fclose($fp);
				if($loadAverage > $this->_global['load_limit'])
				{
					header("HTTP/1.0 503 Service Unavailable");
					readfile(TMPL_PATH . 'Default/http_busy.html');
					exit();
				}
			}
		}

	}

}

/* End of file HomeAction.class.php */
/* Location: ./App/Lib/Action/HomeAction.class.php */